﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYX.Print.Models;
using Dapper;
using XYX.Print.Models.Models;
using System.Security.Principal;
using XYX.Print.Utils;
using System.Security.Cryptography;

namespace XYX.Print.Impl
{
    /// <summary>
    /// 用户
    /// </summary>
    public class SysUserImpl
    {
        /// <summary>
        /// 登陆
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        /// <exception cref="BusinessException"></exception>
        public async Task<bool> Login(string account, string password)
        {
            if (string.IsNullOrEmpty(account))
            {
                throw new BusinessException("帐户不以能为空");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new BusinessException("密码不能为空");
            }
            using (IDbConnection conn = new SqlConnection(DbConnectionManager.ConnString))
            {
                conn.Open();
                string sql = "SELECT  TOP 1 * FROM SysUser WHERE Account=@Account";
                SysUser sysUser = await conn.QueryFirstAsync<SysUser>(sql, new { Account = account });
                if (sysUser == null)
                {
                    throw new BusinessException("用户名不存在");
                }
                if (sysUser.IsDeleted)
                {
                    throw new BusinessException("用户名被删除");
                }
                string pwd = SecurityHelper.MD5(password);
                if (sysUser.PassWord.ToUpper()!= pwd.ToUpper())
                {
                    throw new BusinessException("密码不正确");
                }
                return true;
            }
        }
       
    }
}
